Mixture of Gaussian and Uniform Distribution

First we define a PDF:


In [68]:
from sympy import *
%matplotlib inline
init_printing()
sigma, sigma1, p, mu, mu1, x, a, b  = symbols('sigma, sigma1, p, mu, mu1, x, a, b')
sigma2, mu2 = symbols('sigma2, mu2')
pdf = p * (1 / sigma1) * (exp((-1/2)*((x-mu1)/sigma1)**2)/sqrt(2* pi)) + (1-p) * (1/(b-a))
pdf


Out[68]:
$$\frac{\sqrt{2} p}{2 \sqrt{\pi} \sigma_{1}} e^{- \frac{1}{\sigma_{1}^{2}} \left(- \mu_{1} + x\right)^{2}} + \frac{- p + 1}{- a + b}$$

Let's compute the CDF:


In [64]:
cdf = integrate(pdf,x)
cdf = simplify(cdf)
cdf


Out[64]:
$$\frac{1}{a - b} \left(- \frac{a p}{4} \sqrt{2} \operatorname{erf}{\left (\frac{1}{\sigma_{1}} \left(\mu_{1} - x\right) \right )} + \frac{b p}{4} \sqrt{2} \operatorname{erf}{\left (\frac{1}{\sigma_{1}} \left(\mu_{1} - x\right) \right )} + p x - x\right)$$

Now lets define mu and solve for mu1:


In [70]:
mu_simpli = p * mu1 + (1-p) * ((1/2) * (a+b))
mu1 = solve(Eq(mu_simpli, mu), mu1)
mu1


Out[70]:
$$\left [ \frac{\mu}{p}\right ]$$

In [69]:
sigma_simpli = p * ( (mu1**2) + (sigma1**2)) + (1-p) * ((mu2**2) + (sigma2**2))
sigma1 = solve(Eq(sigma_simpli, sigma), sigma1)
sigma1


Out[69]:
$$\left [ - \sqrt{- \mu_{1}^{2} + \mu_{2}^{2} - \frac{\mu_{2}^{2}}{p} + \sigma_{2}^{2} + \frac{\sigma}{p} - \frac{\sigma_{2}^{2}}{p}}, \quad \sqrt{- \mu_{1}^{2} + \mu_{2}^{2} - \frac{\mu_{2}^{2}}{p} + \sigma_{2}^{2} + \frac{\sigma}{p} - \frac{\sigma_{2}^{2}}{p}}\right ]$$

Now we can calculate sigma1 and mu1 with the simplified versions of mu and sigma


In [13]:
sigma1 = solve(Eq(sigma_simpli, sigma), sigma1)
mu1 = solve(Eq(mu_simpli, mu), mu1)
othersigma =  sigma1 = ( sigma / p) ** (1 / sqrt(2))/2
sigma1, othersigma


Out[13]:
$$\left ( \frac{\left(\frac{\sigma}{p}\right)^{\frac{\sqrt{2}}{2}}}{2}, \quad \frac{\left(\frac{\sigma}{p}\right)^{\frac{\sqrt{2}}{2}}}{2}\right )$$

Let's try it with a mixture of two gaussians:


In [18]:
pdf = p * (1 / sigma1) * (exp((-1/2)*((x-mu1)/sigma1)**2)/sqrt(2* pi)) + (1-p) * (1 / sigma2) * (exp((-1/2)*((x-mu2)/sigma2)**2)/sqrt(2* pi))
pdf


Out[18]:
$$\frac{\sqrt{2} p}{2 \sqrt{\pi} \sigma_{1}} e^{- \frac{1}{\sigma_{1}^{2}} \left(- \mu_{1} + x\right)^{2}} + \frac{\sqrt{2}}{2 \sqrt{\pi} \sigma_{2}} \left(- p + 1\right) e^{- \frac{1}{\sigma_{2}^{2}} \left(- \mu_{2} + x\right)^{2}}$$

In [19]:
cdf = integrate(pdf,x)

In [22]:
cdf = simplify(cdf)
cdf


Out[22]:
$$\frac{\sqrt{2}}{4} \left(- p \operatorname{erf}{\left (\frac{1}{\sigma_{1}} \left(\mu_{1} - x\right) \right )} + p \operatorname{erf}{\left (\frac{1}{\sigma_{2}} \left(\mu_{2} - x\right) \right )} - \operatorname{erf}{\left (\frac{1}{\sigma_{2}} \left(\mu_{2} - x\right) \right )}\right)$$

In [40]:
f = pdf

In [41]:
mu2 = (1/2) * (a+b)

In [42]:
sigma2 = (1/12)*(b-a)**2

In [36]:
delta1 = mu1 - mu
delta2 = mu2 - mu

In [48]:
mean = p* mu1 + (1-p)*mu2
mu1 = mean

In [49]:
variance_simpli = p * (mu1**2 + sigma1**2) + (1-p) * (mu2**2 + sigma2**2)
variance_simpli


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-49-82d6cb1e1535> in <module>()
----> 1 variance_simpli = p * (mu1**2 + sigma1**2) + (1-p) * (mu2**2 + sigma2**2)
      2 variance_simpli

TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'

In [45]:
sigma1 = solve(Eq(variance_simpli, sigma), sigma1)

In [47]:
sigma1, mu1


Out[47]:
$$\left ( \left [ - \sqrt{- \mu_{1}^{2} + \frac{\sigma}{p}}, \quad \sqrt{- \mu_{1}^{2} + \frac{\sigma}{p}}\right ], \quad \mu_{1}\right )$$

In [ ]: